cmake_minimum_required(VERSION 3.20)
project(QtFrontend)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 添加MSVC特定的编译器选项
if(MSVC)
    add_compile_options(/Zc:__cplusplus)
    add_compile_options(/utf-8)
endif()

set(CMAKE_PREFIX_PATH "E:/study_soft/Qt/qt/6.7.0/mingw_64/bin")

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        Network
        WebSockets
        REQUIRED)

# 源文件
set(SOURCES
    main.cpp
    src/mainwindow.cpp
    src/apiclient.cpp
    src/authservice.cpp
    src/gameservice.cpp
    src/gameservicediscovery.cpp
    src/websocketclient.cpp
    src/snakeapiclient.cpp
    # 保留旧管理器（如网络会用到），但删除旧Widget/Renderer
    src/snakegamestate.cpp
    src/snakegamemanager.cpp

    # 移除旧版渲染和界面：snakegamerenderer.cpp / snakegamewidget.cpp / snakegameentrywidget.cpp / singleplayergamewidget.cpp
    # 仅保留现代化入口 modernsnakeentrywidget.cpp
    src/modernsnakeentrywidget.cpp

    # 现代化游戏系统
    src/clientprediction.cpp
    src/gamerenderer.cpp
    src/moderngamecontroller.cpp
    src/asyncnetworkmanager.cpp
    src/gamestateprocessor.cpp
    src/renderthread.cpp

    # 优化架构组件
    src/unifiedgamerenderer.cpp
    src/incrementalgamestateparser.cpp
    src/directdatachannel.cpp
    src/gamedatapool.cpp
    src/networkthread.cpp

    # 保留的高级组件
    src/interpolationengine.cpp
    src/advancedinputhandler.cpp
    src/snakecreateroomdialog.cpp
    src/loginwidget.cpp
    src/registerwidget.cpp
    src/forgotpasswordwidget.cpp
    src/gameserverwidget.cpp
    src/settingswidget.cpp
    src/sidebarwidget.cpp
    src/gameauthprogresswidget.cpp
    src/blankgamewidget.cpp
    src/config.cpp
)

# 头文件
set(HEADERS
        include/mainwindow.h
        include/apiclient.h
        include/authservice.h
        include/gameservice.h
        include/gameservicediscovery.h
        include/websocketclient.h
        include/snakeapiclient.h
        include/snakegamestate.h
        include/snakegamemanager.h

        # 移除旧版头文件：snakegamerenderer.h / snakegamewidget.h / snakegameentrywidget.h / singleplayergamewidget.h
        include/modernsnakeentrywidget.h

        # 现代化游戏系统头文件
        include/clientprediction.h
        include/gamerenderer.h
        include/moderngamecontroller.h
        include/asyncnetworkmanager.h
        include/gamestateprocessor.h
        include/renderthread.h

        # 优化架构组件头文件
        include/unifiedgamerenderer.h
        include/incrementalgamestateparser.h
        include/directdatachannel.h
        include/gamedatapool.h
        include/networkthread.h

        # 保留的高级组件头文件
        include/interpolationengine.h
        include/advancedinputhandler.h
        include/snakecreateroomdialog.h
        include/loginwidget.h
        include/registerwidget.h
        include/forgotpasswordwidget.h
        include/gameserverwidget.h
        include/settingswidget.h
        include/sidebarwidget.h
        include/gameauthprogresswidget.h
        include/blankgamewidget.h
        include/config.h
)

# 资源文件
set(RESOURCES
    resources/resources.qrc
)

add_executable(QtFrontend ${SOURCES} ${HEADERS} ${RESOURCES})

# 设置包含目录
target_include_directories(QtFrontend PRIVATE include)

target_link_libraries(QtFrontend
        Qt::Core
        Qt::Gui
        Qt::Widgets
        Qt::Network
        Qt::WebSockets
)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets Network WebSockets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
